//
//  CSSecondVCBottomView.m
//  ChinaShip
//
//  Created by Yale on 2019/1/27.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSSecondVCBottomView.h"

#define kItemWidth  (int)(kSCREEN_WIDTH/7)

@interface CSSecondVCBottomView (){
    CGRect orginRectLeft3,orginRectLeft2,orginRectLeft1;
    CGRect orginRectRight3,orginRectRight2,orginRectRight1;
    CGRect orginRectMiddle;
}
@property (nonatomic,strong)UIImageView *leftImgView3,*leftImgView2,*leftImgView1;
@property (nonatomic,strong)UIImageView *middleImgView;
@property (nonatomic,strong)UIImageView *rightImgView3,*rightImgView2,*rightImgView1;

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIView       *coverView;
@property (nonatomic,strong)NSArray      *dataArray;
@property (nonatomic,assign)NSInteger    currentPage;
@end

@implementation CSSecondVCBottomView

+ (instancetype)createBottomViewWithFame:(CGRect)frame datas:(NSArray *)datas{
    if (isIPhonex) {
        frame = CGRectMake(frame.origin.x, frame.origin.y-34, frame.size.width, frame.size.height);
    }
    CSSecondVCBottomView *secondBottomView = [[CSSecondVCBottomView alloc]initWithFrame:frame];
    secondBottomView.dataArray = datas;
    [secondBottomView setTapViewWithFrame:frame];
    return secondBottomView;
}

//设置tapView
- (void)setTapViewWithFrame:(CGRect)frame{
    CGRect scrFrame = CGRectZero;
    CGSize contentSize = CGSizeZero;
    if (self.dataArray.count >= 5) {
        scrFrame = CGRectMake(kItemWidth, 0, kItemWidth*5, frame.size.height);
        contentSize = CGSizeMake(kSCREEN_WIDTH, 0);
    }else{
        scrFrame = CGRectMake(kItemWidth*2, 0, kItemWidth*3, frame.size.height);
        contentSize = CGSizeMake(kItemWidth*5, 0);
    }
    UIScrollView *scrView = [[UIScrollView alloc]initWithFrame:scrFrame];
    scrView.scrollEnabled = NO;
    scrView.showsVerticalScrollIndicator = NO;
    scrView.showsHorizontalScrollIndicator = NO;
    scrView.contentSize = contentSize;
    [self addSubview:scrView];
    self.scrollView = scrView;
    [self setUpSubViewWithFrame:frame];
    self.coverView = [[UIView alloc]initWithFrame:self.scrollView.frame];
    self.coverView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.coverView];
    
    UISwipeGestureRecognizer *leftSwipeGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    leftSwipeGes.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.coverView addGestureRecognizer:leftSwipeGes];
    
    UISwipeGestureRecognizer *rightSwipeGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    rightSwipeGes.direction = UISwipeGestureRecognizerDirectionRight;
    [self.coverView addGestureRecognizer:rightSwipeGes];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureView:)];
    [self.coverView addGestureRecognizer:tap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(swipeNotiofaction:) name:kMoveDirectionNotifation object:nil];
    self.currentPage = 0;
}

- (void)swipeNotiofaction:(NSNotification *)notification{
    if ([notification.object isKindOfClass:[UISwipeGestureRecognizer class]]) {
        UISwipeGestureRecognizer *swipeG = (UISwipeGestureRecognizer *)notification.object;
//       [self moveDirection:swipeG.direction];
        [self goMove:swipeG.direction];
    }
}

//左右滑动
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)swipeGes{
    if (self.dataArray.count == 1) {
        return;
    }
    if (self.moveDirectionBlock) {
        self.moveDirectionBlock(swipeGes,100000);
    }
//    [self moveDirection:swipeGes.direction];
    [self goMove:swipeGes.direction];
}

//单击某个视图
- (void)tapGestureView:(UITapGestureRecognizer *)tap{
    if (self.dataArray.count == 1) {
        return;
    }
    CGPoint tapPoint = [tap locationInView:tap.view];
    if (CGRectContainsPoint(orginRectLeft2, tapPoint)){
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] init];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        [self moveTwoDirection:UISwipeGestureRecognizerDirectionRight];
        if (self.moveDirectionBlock) {
            self.moveDirectionBlock(swipeGesture,self.currentPage);
        }
        
    }else if (CGRectContainsPoint(orginRectLeft1, tapPoint)){
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] init];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        if (self.moveDirectionBlock) {
            self.moveDirectionBlock(swipeGesture,100000);
        }
//        [self moveDirection:UISwipeGestureRecognizerDirectionRight];
        [self goMove:UISwipeGestureRecognizerDirectionRight];
    }else if (CGRectContainsPoint(orginRectMiddle, tapPoint)){
        NSLog(@"--tap:---中间--");
    }else if (CGRectContainsPoint(orginRectRight1, tapPoint)){
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] init];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
        if (self.moveDirectionBlock) {
            self.moveDirectionBlock(swipeGesture,100000);
        }
//        [self moveDirection:UISwipeGestureRecognizerDirectionLeft];
        [self goMove:UISwipeGestureRecognizerDirectionLeft];
    }else if (CGRectContainsPoint(orginRectRight2, tapPoint)){
        
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] init];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
        [self moveTwoDirection:UISwipeGestureRecognizerDirectionLeft];
        if (self.moveDirectionBlock) {
            self.moveDirectionBlock(swipeGesture,self.currentPage);
        }
    }
}

- (void)goMove:(UISwipeGestureRecognizerDirection)direction{
    if (self.dataArray.count >= 5) {
        [self moveDirection:direction];
    }else{
        if (self.dataArray.count == 1) {
            return;
        }
        [self threeMoveDirection:direction];
    }
}


- (void)setUpSubViewWithFrame:(CGRect)frame{
    NSInteger totalCount = self.dataArray.count;
    NSInteger height = 40;
    NSInteger tuHeight = 75;
    CGFloat y = CGRectGetHeight(frame)-height;
    
    if (self.dataArray.count >= 5) {
        
        orginRectLeft3 = CGRectMake(-kItemWidth, y, kItemWidth, tuHeight);
        UIImageView *leftImgView3 = [[UIImageView alloc]initWithFrame:orginRectLeft3];
        leftImgView3.tag = 10;
        [self setCornerRadiusStyle:leftImgView3];
        [self.scrollView addSubview:leftImgView3];
        _leftImgView3 = leftImgView3;
        
        
        orginRectLeft2 = CGRectMake(0, y, kItemWidth, tuHeight);
        UIImageView *leftImgView2 = [[UIImageView alloc]initWithFrame:orginRectLeft2];
        leftImgView2.tag = 11;
        [self setCornerRadiusStyle:leftImgView2];
//        leftImgView2.image = [UIImage imageNamed:self.dataArray[totalCount-2]];
        [leftImgView2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[totalCount-2]]];
        [self.scrollView addSubview:leftImgView2];
        _leftImgView2 = leftImgView2;
        
        
        orginRectLeft1 = CGRectMake(kItemWidth, y, kItemWidth, tuHeight);
        UIImageView *leftImgView1 = [[UIImageView alloc]initWithFrame:orginRectLeft1];
        leftImgView1.tag = 12;
        [self setCornerRadiusStyle:leftImgView1];
//        leftImgView1.image = [UIImage imageNamed:self.dataArray.lastObject];
        [leftImgView1 sd_setImageWithURL:[NSURL URLWithString:self.dataArray.lastObject]];
        [self.scrollView addSubview:leftImgView1];
        _leftImgView1 = leftImgView1;
        
        orginRectMiddle = CGRectMake(kItemWidth*2, y-35, kItemWidth, tuHeight);
        UIImageView *middleView = [[UIImageView alloc]initWithFrame:orginRectMiddle];
        middleView.tag = 13;
        [self setCornerRadiusStyle:middleView];
//        middleView.image = [UIImage imageNamed:self.dataArray.firstObject];
        [middleView sd_setImageWithURL:[NSURL URLWithString:self.dataArray.firstObject]];
        [self.scrollView addSubview:middleView];
        _middleImgView = middleView;
        
        orginRectRight1 = CGRectMake(kItemWidth*3, y, kItemWidth, tuHeight);
        UIImageView *rightView1 = [[UIImageView alloc]initWithFrame:orginRectRight1];
        rightView1.tag = 14;
        [self setCornerRadiusStyle:rightView1];
//        rightView1.image = [UIImage imageNamed:self.dataArray[1]];
        [rightView1 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[1]]];
        [self.scrollView addSubview:rightView1];
        _rightImgView1 = rightView1;
        
        orginRectRight2 = CGRectMake(kItemWidth*4, y, kItemWidth, tuHeight);
        UIImageView *rightView2 = [[UIImageView alloc]initWithFrame:orginRectRight2];
        rightView2.tag = 15;
        [self setCornerRadiusStyle:rightView2];
//        rightView2.image = [UIImage imageNamed:self.dataArray[2]];
        [rightView2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[2]]];
        [self.scrollView addSubview:rightView2];
        rightView2 = rightView2;
        
        orginRectRight3 = CGRectMake(kItemWidth*5, y, kItemWidth, tuHeight);
        UIImageView *rightView3 = [[UIImageView alloc]initWithFrame:orginRectRight3];
        rightView3.tag = 16;
        [self setCornerRadiusStyle:rightView3];
        [self.scrollView addSubview:rightView3];
        _rightImgView3 = rightView3;
    }else{
        
        orginRectLeft2 = CGRectMake(-kItemWidth, y, kItemWidth, tuHeight);
        UIImageView *leftImgView2 = [[UIImageView alloc]initWithFrame:orginRectLeft2];
        leftImgView2.tag = 11;
        [self setCornerRadiusStyle:leftImgView2];
        [self.scrollView addSubview:leftImgView2];
        _leftImgView2 = leftImgView2;
        
        
        orginRectLeft1 = CGRectMake(0, y, kItemWidth, tuHeight);
        UIImageView *leftImgView1 = [[UIImageView alloc]initWithFrame:orginRectLeft1];
        leftImgView1.tag = 12;
        [self setCornerRadiusStyle:leftImgView1];
//        leftImgView1.image = [UIImage imageNamed:self.dataArray.lastObject];
        [leftImgView1 sd_setImageWithURL:[NSURL URLWithString:self.dataArray.lastObject]];
        [self.scrollView addSubview:leftImgView1];
        _leftImgView1 = leftImgView1;
        
        orginRectMiddle = CGRectMake(kItemWidth, y-35, kItemWidth, tuHeight);
        UIImageView *middleView = [[UIImageView alloc]initWithFrame:orginRectMiddle];
        middleView.tag = 13;
        [self setCornerRadiusStyle:middleView];
//        middleView.image = [UIImage imageNamed:self.dataArray.firstObject];
        [middleView sd_setImageWithURL:[NSURL URLWithString:self.dataArray.firstObject]];
        [self.scrollView addSubview:middleView];
        _middleImgView = middleView;
        
        orginRectRight1 = CGRectMake(kItemWidth*2, y, kItemWidth, tuHeight);
        UIImageView *rightView1 = [[UIImageView alloc]initWithFrame:orginRectRight1];
        rightView1.tag = 14;
        [self setCornerRadiusStyle:rightView1];
//        rightView1.image = [UIImage imageNamed:self.dataArray[1]];
        [rightView1 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[1]]];
        [self.scrollView addSubview:rightView1];
        _rightImgView1 = rightView1;
        
        orginRectRight2 = CGRectMake(kItemWidth*3, y, kItemWidth, tuHeight);
        UIImageView *rightView2 = [[UIImageView alloc]initWithFrame:orginRectRight2];
        rightView2.tag = 15;
        [self setCornerRadiusStyle:rightView2];
        [self.scrollView addSubview:rightView2];
        rightView2 = rightView2;
        
        if (self.dataArray.count == 1) {
            _leftImgView1.hidden = YES;
            _rightImgView1.hidden = YES;
        }
    }
}


//三视图专门移动
- (void)threeMoveDirection:(UISwipeGestureRecognizerDirection)direction{
    UIImageView *currentLeftImg2 = [self getleftImgView2];
    UIImageView *currentLeftImg1 = [self getleftImgView1];
    UIImageView *currentMiddleImg = [self getMiddleImgView];
    UIImageView *currentRightImg1 = [self getRightImgView1];
    UIImageView *currentRightImg2 = [self getRightImgView2];

    __block typeof(orginRectRight2)weakOrginRectRight2 = orginRectRight2;
    __block typeof(orginRectRight1)weakOrginRectRight1 = orginRectRight1;
    __block typeof(orginRectMiddle)weakOrginRectMiddle = orginRectMiddle;
    __block typeof(orginRectLeft2)weakOrginRectLeft2 = orginRectLeft2;
    __block typeof(orginRectLeft1)weakOrginRectLeft1 = orginRectLeft1;
    
    if (direction == UISwipeGestureRecognizerDirectionLeft) {
        if (self.currentPage == self.dataArray.count-1) {
            self.currentPage = 0;
        }else{
            self.currentPage++;
        }
        [UIView animateWithDuration:0.3 animations:^{
            currentRightImg2.frame = weakOrginRectRight1;
            currentRightImg1.frame = weakOrginRectMiddle;
        
            currentMiddleImg.frame = weakOrginRectLeft1;
            
            currentLeftImg1.frame = weakOrginRectLeft2;
            currentLeftImg2.frame = weakOrginRectRight2;
        
            if (self.currentPage+1<self.dataArray.count) {
//                currentRightImg2.image = [UIImage imageNamed:self.dataArray[self.currentPage+1]];
                [currentRightImg2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage+1]]];
            }else{
                int  i = abs((int)(self.currentPage+1-self.dataArray.count));
                [currentRightImg2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[i]]];
//                currentRightImg2.image = [UIImage imageNamed:self.dataArray[i]];
            }
        }completion:^(BOOL finished) {
            
            if (self.currentPage+2<self.dataArray.count) {
//                currentLeftImg2.image = [UIImage imageNamed:self.dataArray[self.currentPage+2]];
                [currentLeftImg2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage+2]]];
            }else{
                int  i = abs((int)(self.currentPage+2-self.dataArray.count));
//                currentLeftImg2.image = [UIImage imageNamed:self.dataArray[i]];
                [currentLeftImg2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[i]]];
            }
            
            currentLeftImg2.alpha = 1.0f;
        }];
        currentLeftImg2.alpha = 0.0f;
    }else{
        if (self.currentPage == 0) {
            self.currentPage = self.dataArray.count-1;
        }else{
            self.currentPage--;
        }
        [UIView animateWithDuration:0.3 animations:^{
            currentRightImg2.frame = weakOrginRectLeft2;
            currentRightImg1.frame = weakOrginRectRight2;
            
            currentMiddleImg.frame = weakOrginRectRight1;
            
            currentLeftImg1.frame = weakOrginRectMiddle;
            currentLeftImg2.frame = weakOrginRectLeft1;
            
            if (self.currentPage-1 < 0) {
                int index = abs((int)(self.currentPage-1));
//                currentLeftImg2.image = [UIImage imageNamed:self.dataArray[self.dataArray.count - index]];
                [currentLeftImg2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.dataArray.count - index]]];
            }else{
//                currentLeftImg2.image = [UIImage imageNamed:self.dataArray[self.currentPage-1]];
                [currentLeftImg2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage-1]]];
            }
            
        }completion:^(BOOL finished) {
            //       5 0 1 2 3
            if (self.currentPage-1 < 0) {
                int index = abs((int)(self.currentPage-2));
//                currentRightImg2.image = [UIImage imageNamed:self.dataArray[self.dataArray.count -index]];
                [currentRightImg2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.dataArray.count -index]]];
            }else{
//                currentRightImg2.image = [UIImage imageNamed:self.dataArray[self.currentPage-1]];
                [currentRightImg2 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage-1]]];
            }
            
            currentRightImg2.alpha = 1.0f;
        }];
        currentRightImg2.alpha = 0.0;
    }
    
}

//=====================================================/*五视图以上的动画*/=====================================================


//移动代理
- (void)moveDirection:(UISwipeGestureRecognizerDirection)direction{
    UIImageView *currentLeftImg3 = [self getleftImgView3];
    UIImageView *currentLeftImg2 = [self getleftImgView2];
    UIImageView *currentLeftImg1 = [self getleftImgView1];
    UIImageView *currentMiddleImg = [self getMiddleImgView];
    UIImageView *currentRightImg3 = [self getRightImgView3];
    UIImageView *currentRightImg2 = [self getRightImgView2];
    UIImageView *currentRightImg1 = [self getRightImgView1];
    
    __block typeof(orginRectRight3)weakOrginRectRight3 = orginRectRight3;
    __block typeof(orginRectRight2)weakOrginRectRight2 = orginRectRight2;
    __block typeof(orginRectRight1)weakOrginRectRight1 = orginRectRight1;
    
    __block typeof(orginRectMiddle)weakOrginRectMiddle = orginRectMiddle;
    
    __block typeof(orginRectLeft3)weakOrginRectLeft3 = orginRectLeft3;
    __block typeof(orginRectLeft2)weakOrginRectLeft2 = orginRectLeft2;
    __block typeof(orginRectLeft1)weakOrginRectLeft1 = orginRectLeft1;
    
    if (direction == UISwipeGestureRecognizerDirectionLeft) {
        if (self.currentPage == self.dataArray.count-1) {
            self.currentPage = 0;
        }else{
            self.currentPage++;
        }
        [UIView animateWithDuration:0.3 animations:^{
            currentRightImg3.frame = weakOrginRectRight2;
            currentRightImg2.frame = weakOrginRectRight1;
            currentRightImg1.frame = weakOrginRectMiddle;
            currentMiddleImg.frame = weakOrginRectLeft1;
            currentLeftImg1.frame = weakOrginRectLeft2;
            currentLeftImg2.frame = weakOrginRectLeft3;
            currentLeftImg3.frame = weakOrginRectRight3;
            if (self.currentPage+2<self.dataArray.count) {
//                currentRightImg3.image = [UIImage imageNamed:self.dataArray[self.currentPage+2]];
                [currentRightImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage+2]]];
            }else{
                int  i = abs((int)(self.currentPage+2-self.dataArray.count));
//                currentRightImg3.image = [UIImage imageNamed:self.dataArray[i]];
                [currentRightImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[i]]];
            }
        }completion:^(BOOL finished) {
            
            if (self.currentPage+3<self.dataArray.count) {
//                currentLeftImg3.image = [UIImage imageNamed:self.dataArray[self.currentPage+3]];
                [currentLeftImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage+3]]];
            }else{
                int  i = abs((int)(self.currentPage+3-self.dataArray.count));
//                currentLeftImg3.image = [UIImage imageNamed:self.dataArray[i]];
                [currentLeftImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[i]]];
            }
            
            currentLeftImg3.alpha = 1.0f;
        }];
        currentLeftImg3.alpha = 0.0f;
    }else{
        if (self.currentPage == 0) {
            self.currentPage = self.dataArray.count-1;
        }else{
            self.currentPage--;
        }
        [UIView animateWithDuration:0.3 animations:^{
            currentRightImg2.frame = weakOrginRectRight3;
            currentRightImg1.frame = weakOrginRectRight2;
            currentMiddleImg.frame = weakOrginRectRight1;
            
            currentLeftImg1.frame = weakOrginRectMiddle;
            
            currentLeftImg2.frame = weakOrginRectLeft1;
            currentLeftImg3.frame = weakOrginRectLeft2;
            currentRightImg3.frame = weakOrginRectLeft3;
            
            if (self.currentPage-2 < 0) {
                int index = abs((int)(self.currentPage-2));
//                currentLeftImg3.image = [UIImage imageNamed:self.dataArray[self.dataArray.count - index]];
                [currentLeftImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.dataArray.count - index]]];
            }else{
//                currentLeftImg3.image = [UIImage imageNamed:self.dataArray[self.currentPage-2]];
                [currentLeftImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage-2]]];
            }
            
        }completion:^(BOOL finished) {
//       5 0 1 2 3
            if (self.currentPage-2 < 0) {
                int index = abs((int)(self.currentPage-3));
//                currentRightImg3.image = [UIImage imageNamed:self.dataArray[self.dataArray.count -index]];
                [currentRightImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.dataArray.count -index]]];
            }else{
//                currentRightImg3.image = [UIImage imageNamed:self.dataArray[self.currentPage-2]];
                [currentRightImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage-2]]];
            }
            
            currentRightImg3.alpha = 1.0f;
        }];
        currentRightImg3.alpha = 0.0;
    }
}

//点击后连续滚动两次
- (void)moveTwoDirection:(UISwipeGestureRecognizerDirection)direction{
    UIImageView *currentLeftImg3 = [self getleftImgView3];
    UIImageView *currentLeftImg2 = [self getleftImgView2];
    UIImageView *currentLeftImg1 = [self getleftImgView1];
    UIImageView *currentMiddleImg = [self getMiddleImgView];
    UIImageView *currentRightImg3 = [self getRightImgView3];
    UIImageView *currentRightImg2 = [self getRightImgView2];
    UIImageView *currentRightImg1 = [self getRightImgView1];
    
    __block typeof(orginRectRight3)weakOrginRectRight3 = orginRectRight3;
    __block typeof(orginRectRight2)weakOrginRectRight2 = orginRectRight2;
    __block typeof(orginRectRight1)weakOrginRectRight1 = orginRectRight1;
    
    __block typeof(orginRectMiddle)weakOrginRectMiddle = orginRectMiddle;
    
    __block typeof(orginRectLeft3)weakOrginRectLeft3 = orginRectLeft3;
    __block typeof(orginRectLeft2)weakOrginRectLeft2 = orginRectLeft2;
    __block typeof(orginRectLeft1)weakOrginRectLeft1 = orginRectLeft1;
    
    if (direction == UISwipeGestureRecognizerDirectionLeft) {
        self.currentPage+=2;
        if (self.currentPage > self.dataArray.count-1) {
            self.currentPage = abs((int)(self.currentPage-self.dataArray.count));
        }
        [UIView animateWithDuration:0.3 animations:^{
            
            currentMiddleImg.frame = weakOrginRectLeft2;
            
            currentLeftImg1.frame = weakOrginRectLeft3;
            currentLeftImg2.frame = weakOrginRectRight3;
            currentLeftImg3.frame = weakOrginRectRight2;
            
            currentRightImg1.frame = weakOrginRectLeft1;
            currentRightImg2.frame = weakOrginRectMiddle;
            currentRightImg3.frame = weakOrginRectRight1;
            
            
            if (self.currentPage < self.dataArray.count-1) {
//                currentRightImg3.image = [UIImage imageNamed:self.dataArray[self.currentPage+1]];
                [currentRightImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage+1]]];
            }else{
                int  i = abs((int)(self.currentPage+1-self.dataArray.count));
//                currentRightImg3.image = [UIImage imageNamed:self.dataArray[i]];
                [currentRightImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[i]]];
            }
            

            if (self.currentPage+2<self.dataArray.count) {
//                currentLeftImg3.image = [UIImage imageNamed:self.dataArray[self.currentPage+2]];
                [currentLeftImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage+2]]];
            }else{
                int  i = abs((int)(self.currentPage+2-self.dataArray.count));
//                currentLeftImg3.image = [UIImage imageNamed:self.dataArray[i]];
                [currentLeftImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[i]]];
            }
            
        }completion:^(BOOL finished) {
            currentLeftImg2.alpha = 1.0f;
            currentLeftImg3.alpha = 1.0f;
        }];
        currentLeftImg2.alpha = 0.0f;
        currentLeftImg3.alpha = 0.0f;
    }else{
        
        self.currentPage -= 2;
        if (self.currentPage < 0) {
            self.currentPage = self.dataArray.count - abs((int)(self.currentPage));
        }
        
        [UIView animateWithDuration:0.3 animations:^{
            
            currentLeftImg3.frame = weakOrginRectLeft1;
            currentLeftImg2.frame = weakOrginRectMiddle;
            currentLeftImg1.frame = weakOrginRectRight1;
            
            currentMiddleImg.frame = weakOrginRectRight2;
            
            currentRightImg1.frame = weakOrginRectRight3;
            currentRightImg2.frame = weakOrginRectLeft3;
            currentRightImg3.frame = weakOrginRectLeft2;
            
            
            /*
             page
             3  4  0  1  2         0 - 2   -2
             1  2  3  4  0         3 - 2   1
             4  0  1  2  3         1 - 2   -1
             2  3  4  0  1         4 - 2   2
             
             < 0  --->  total - |-page|
             */
            if (self.currentPage-1 < 0) {
                int index = abs((int)(self.dataArray.count - self.currentPage - 1));
//                currentLeftImg3.image = [UIImage imageNamed:self.dataArray[index]];
                [currentLeftImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[index]]];
            }else{
//                currentLeftImg3.image = [UIImage imageNamed:self.dataArray[self.currentPage-1]];
                [currentLeftImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage-1]]];
            }
            
            if (self.currentPage-2 < 0) {
                int index = abs((int)(self.dataArray.count - abs((int)(self.currentPage - 2))));
//                currentRightImg3.image = [UIImage imageNamed:self.dataArray[index]];
                [currentRightImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[index]]];
            }else{
//                currentRightImg3.image = [UIImage imageNamed:self.dataArray[self.currentPage-2]];
                [currentRightImg3 sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.currentPage-2]]];
            }

        }completion:^(BOOL finished) {
            currentRightImg2.alpha = 1.0f;
            currentRightImg3.alpha = 1.0f;
        }];
        currentRightImg2.alpha = 0.0f;
        currentRightImg3.alpha = 0.0f;
        
    }
}




//   3  4  5  0  1  2  3
- (UIImageView *)getleftImgView3{
    UIImageView *leftImgView3 = nil;
    for (NSInteger index = 10; index < 17; index ++ ) {
        UIImageView *tempImgView = [self.scrollView viewWithTag:index];
        if (CGRectEqualToRect(tempImgView.frame, orginRectLeft3)) {
            leftImgView3 = tempImgView;
        }
    }
    return leftImgView3;
}

- (UIImageView *)getleftImgView2{
    UIImageView *leftImgView2 = nil;
    for (NSInteger index = 10; index < 17; index ++ ) {
        UIImageView *tempImgView = [self.scrollView viewWithTag:index];
        if (CGRectEqualToRect(tempImgView.frame, orginRectLeft2)) {
            leftImgView2 = tempImgView;
        }
    }
    return leftImgView2;
}

- (UIImageView *)getleftImgView1{
    UIImageView *leftImgView1 = nil;
    for (NSInteger index = 10; index < 17; index ++ ) {
        UIImageView *tempImgView = [self.scrollView viewWithTag:index];
        if (CGRectEqualToRect(tempImgView.frame, orginRectLeft1)) {
            leftImgView1 = tempImgView;
        }
    }
    return leftImgView1;
}

- (UIImageView *)getMiddleImgView{
    UIImageView *middleImgView = nil;
    for (NSInteger index = 10; index < 17; index ++ ) {
        UIImageView *tempImgView = [self.scrollView viewWithTag:index];
        if (CGRectEqualToRect(tempImgView.frame, orginRectMiddle)) {
            middleImgView = tempImgView;
        }
    }
    return middleImgView;
}


- (UIImageView *)getRightImgView1{
    UIImageView *rightImgView1 = nil;
    for (NSInteger index = 10; index < 17; index ++ ) {
        UIImageView *tempImgView = [self.scrollView viewWithTag:index];
        if (CGRectEqualToRect(tempImgView.frame, orginRectRight1)) {
            rightImgView1 = tempImgView;
        }
    }
    return rightImgView1;
}


- (UIImageView *)getRightImgView2{
    UIImageView *rightImgView2 = nil;
    for (NSInteger index = 10; index < 17; index ++ ) {
        UIImageView *tempImgView = [self.scrollView viewWithTag:index];
        if (CGRectEqualToRect(tempImgView.frame, orginRectRight2)) {
            rightImgView2 = tempImgView;
        }
    }
    return rightImgView2;
}

- (UIImageView *)getRightImgView3{
    UIImageView *rightImgView3 = nil;
    for (NSInteger index = 10; index < 17; index ++ ) {
        UIImageView *tempImgView = [self.scrollView viewWithTag:index];
        if (CGRectEqualToRect(tempImgView.frame, orginRectRight3)) {
            rightImgView3 = tempImgView;
        }
    }
    return rightImgView3;
}


- (void)setCornerRadiusStyle:(UIImageView *)imgView{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imgView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = imgView.bounds;
    maskLayer.path = maskPath.CGPath;
    imgView.layer.mask = maskLayer;
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
